import {Button, Col, Row, Space, Table, Typography} from "antd";
import {useEffect, useState} from "react";
import {invoke} from "@tauri-apps/api/core";

import {useLocation, useNavigate} from 'react-router-dom';

const AppHome = () => {
    const navigate = useNavigate();
    const [listData, setListData] = useState([])
    const [ckPathList, setCkPathList] = useState([])
    const {state} = useLocation()
    useEffect(() => {
        listDir(state.diskPath)
    }, []);
    const listDir = (path) => {
        invoke("list_dir_by_path", {path}).then(res => {
            console.log("listdir>>res:", res)
            setCkPathList([])
            setListData(res)
        })
    }
    const toLargeP = () => {
        navigate(`/file/large`, {
            state: {
                paths: ckPathList.map(p => state.diskPath + p)
                , diskPath: state.diskPath
            }
        });
    }
    const toRepeatP = () => {
        navigate(`/file/repeat`, {
            state: {
                paths: ckPathList.map(p => state.diskPath + p)
                , diskPath: state.diskPath
            }
        });
    }
    return (
        <div style={{}}>
            <Row>
                <Col span={24}>
                    <Row>
                        <Col span={2}>
                            <Button onClick={() => {
                                window.history.back();
                            }}>返回</Button>
                        </Col>
                        <Col span={14}>
                            {ckPathList.length > 0 ?
                                <span>已选择<Typography.Text style={{fontSize: 16}}
                                                             code>
                                                                {ckPathList.length}
                                                        </Typography.Text>
                                                        个文件夹</span>
                                : ""}
                        </Col>
                        <Col span={8} style={{textAlign: "right"}}>
                            <Space size={"large"}>
                                <Button type='primary' onClick={toLargeP}>查找大文件</Button>
                                <Button type='primary' onClick={toRepeatP}>查找重复文件</Button>
                            </Space>
                        </Col>
                    </Row>
                    <Row>
                        <Col span={24}>
                            <div style={{alignItems: "center", marginBottom: '8px'}}>
                            </div>
                            <Table
                                size={"middle"}
                                bordered={true}
                                search={false}
                                options={false}
                                pagination={false}
                                dataSource={listData}
                                // showHeader={false}
                                rowSelection={{
                                    onChange: (selectedRowKeys, selectedRows) => {
                                        setCkPathList(selectedRowKeys)
                                    }
                                }}
                                rowKey={"path"}
                                columns={[
                                    {
                                        key: 'path',
                                        dataIndex: 'path',
                                        title: `${state.diskPath} 盘`,
                                        ellipsis: true,
                                    },
                                ]}
                            >
                            </Table>
                        </Col>
                    </Row>
                </Col></Row>
        </div>
    )
}
export default AppHome;